package acheng1314.cn.a3dbuild2.view.activity;


import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.acheng.achengutils.mvp.view.BaseActivity;
import com.acheng.achengutils.utils.SPHelper;
import com.acheng.achengutils.widgets.MustDoThingDailog;

import java.util.ArrayList;
import java.util.List;

import acheng1314.cn.a3dbuild2.MyApplication;
import acheng1314.cn.a3dbuild2.R;
import acheng1314.cn.a3dbuild2.adapter.ProjectListAdapter;
import acheng1314.cn.a3dbuild2.bean.ProjectItemBean;
import acheng1314.cn.a3dbuild2.service.LocationService;
import acheng1314.cn.a3dbuild2.view.activity.presenter.HomeActivityPresenter;
import acheng1314.cn.a3dbuild2.view.activity.viewcontroller.HomeActivityViewController;
import acheng1314.cn.a3dbuild2.widgets.MyProgressDialog;

public class HomeActivity extends BaseActivity<HomeActivityPresenter, HomeActivityViewController> implements HomeActivityViewController {

    private ListView mLv_project_home;  //首页的工程列表
    private MyProgressDialog myProgressDialog;
    private ArrayList<ProjectItemBean.ResultBean> mListData = new ArrayList<>();   //数据源
    private ProjectListAdapter adapter; //列表适配器
    private Intent bindIntent;

    @Override
    protected HomeActivityPresenter initPresenter() {
        return new HomeActivityPresenter(this);
    }


    @Override
    protected void initEvent() {

        mLv_project_home.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ProjectItemBean.ResultBean bean = mListData.get(position);
                Intent startIntent = new Intent(getContext(), DetailActivity.class);
                startIntent.putExtra(getString(R.string.user), bean);
                startActivity(startIntent);
            }
        });


    }

    @Override
    protected void initView() {
        setMyTitle();
        mLv_project_home = (ListView) findViewById(R.id.mLv_project_home);

        adapter = new ProjectListAdapter(mListData, getContext());

        mLv_project_home.setAdapter(adapter);

//        getData4Net();

    }

    private void getData4Net() {
        //add user into bundle
        String token = SPHelper.getString(getContext(), getString(R.string.user), getString(R.string.token));
        String userId = SPHelper.getString(getContext(), getString(R.string.user), getString(R.string.userId));

        mPresenter.loadListData(userId, token);
    }

    private void setMyTitle() {
        String titleStr = String.format("用户：%s", SPHelper.getString(getContext(), getString(R.string.user), getString(R.string.username), "默认用户"));
        setTitle(titleStr);
    }

    @Override
    protected void onResume() {
        super.onResume();
        setMyTitle();
        getData4Net();
        bindIntent = new Intent(getContext(), LocationService.class);
        startService(bindIntent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != bindIntent) {
            stopService(bindIntent);
        }
    }

    @Override
    protected int setLayoutId() {
        return R.layout.activity_home;
    }

    @Override
    public void showProgressD() {
        if (null == myProgressDialog)
            myProgressDialog = new MyProgressDialog("加载中", "正在加载数据···", getContext());
        else
            myProgressDialog.show();
    }

    @Override
    public void disProgressD() {
        if (null != myProgressDialog)
            myProgressDialog.dismiss();
    }

    @Override
    public void showDailog(String msg) {
        new MustDoThingDailog("提示", msg, getContext(), new MustDoThingDailog.NeedDoThing() {
            @Override
            public void mustDoThings() {

            }
        });
    }

    @Override
    public void updateList(List<ProjectItemBean.ResultBean> listData) {
        if (mListData == null) {
            mListData = new ArrayList<>();
        }

        if (adapter == null) {
            adapter = new ProjectListAdapter(mListData, getContext());
        }

        if (mListData.size() > 0) {
            mListData.clear();
            mListData.addAll(listData);
        } else {
            mListData.addAll(listData);
        }

        //update data
//        adapter.update(mListData);
        adapter.notifyDataSetChanged();

        MyApplication.getInstance().outLog("输出", "size:\t" + adapter.getCount());

    }


}
